package com.itheima.sfbx.insurance.handler.impl;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.sfbx.framework.commons.constant.insure.InsureConstant;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.insurance.dto.CustomerRelationVO;
import com.itheima.sfbx.insurance.dto.DoInsureVo;
import com.itheima.sfbx.insurance.dto.InsuranceVO;
import com.itheima.sfbx.insurance.enums.InsuranceEnum;
import com.itheima.sfbx.insurance.handler.InsureHandler;
import com.itheima.sfbx.insurance.service.ICustomerRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @ClassName AccidentInsureHandler.java
 * @Description 保障型意外类：保险投保、创建订单、保费计算
 */
@Service
public class AccidentInsureHandler extends SafeguardInsureHandler implements InsureHandler {


    @Resource
    private ICustomerRelationService customerRelationService;

    @Override
    public String doPremium(DoInsureVo doInsureVo) {
        //1、当前这份保险是给谁买的?找到被投保人
        String customerRelationId = doInsureVo.getCustomerRelationIds().get(0);
        CustomerRelationVO customerRelationVO = customerRelationService.findById(customerRelationId);
        //2、查看被保人的月收入是否为空、或者超过1000000 那么则抛出异常
        if (ObjectUtil.isNotNull(customerRelationVO)) { // Hutool工具类封装, 更具"语义化"
//        if (customerRelationVO != null) {   java云生语法, 最直接、最高效
            if (customerRelationVO.getIncome() == null ||
                    customerRelationVO.getIncome().compareTo(new BigDecimal(1000000)) > 0) {
                //记忆: 前者 - 后者 即可
                throw new RuntimeException("杀手不可以买以外保险");
            }
        }
        return super.doPremium(doInsureVo);
    }
}
